package com.vdong.common.oauth.web.controller;

import com.vdong.common.oauth.common.authorize.AuthCommon;
import com.vdong.common.oauth.common.util.CopyUtil;
import com.vdong.common.oauth.common.util.CommonResult;
import com.vdong.common.oauth.common.util.Result;
import com.vdong.common.oauth.entity.dto.LoginParamDTO;
import com.vdong.common.oauth.entity.dto.LoginResultDTO;
import com.vdong.common.oauth.entity.dto.PeddleUserDTO;
import com.vdong.common.oauth.entity.mo.AppUserLoginInfo;
import com.vdong.common.oauth.entity.po.LoginParamPO;
import com.vdong.common.oauth.entity.vo.UserLoginVO;
import com.vdong.common.oauth.service.LoginService;
import com.vdong.kpay.basics.dubbo.result.APIResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/loginH5", method = RequestMethod.POST)
    @ResponseBody
    public Result<UserLoginVO> loginH5(@RequestParam @NotNull String code) {
        UserLoginVO userLoginVO = loginService.loginH5Execute(code);
        return Result.success(userLoginVO);
    }

    @RequestMapping(value = "/execute", method = RequestMethod.POST)
    @ResponseBody
    public Result<CommonResult> execute(@RequestBody @Valid @NotNull LoginParamPO param) {
        try {
            CommonResult<String> result = loginService.execute(CopyUtil.copyObject(param, new LoginParamDTO()));
            if (!result.isSuccess()) {
                return Result.fail(result.getErrorMsg());
            }
            return Result.success(result);
        } catch (Exception e) {
            log.info("登陆接口异常:[{}]", e);
            return Result.fail(e.toString());
        }
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage() {
        return "login";
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public APIResult success() {
        return AuthCommon.SUCCESS_AUTH_RESULT;
    }

    @RequestMapping("/exit")
    public void exit(HttpServletRequest request, HttpServletResponse response) {
        // token can be revoked here if needed
        new SecurityContextLogoutHandler().logout(request, null, null);
        try {
            //sending back to client app
            String redirectUrl = request.getParameter("redirect_url");
            response.sendRedirect(redirectUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    @ApiOperation(value = "登录")
    @ResponseBody
    public PeddleUserDTO doLogin() {
        AppUserLoginInfo peddleUserDTO = new AppUserLoginInfo();
        peddleUserDTO.setOpenId("1234");
        peddleUserDTO.setUnionId("12345");
        peddleUserDTO.setGender(1);
        peddleUserDTO.setNickName("zs");
        peddleUserDTO.setAvatarUrl("1.png");
        PeddleUserDTO userLoginVO = loginService.doLogin(peddleUserDTO);
        return userLoginVO;
    }
}
